package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMSignUp;

/**
 * Created by zlz
 * on  5/11/21
 */
public class ACSignUp extends BaseActivity implements View.OnClickListener, TextWatcher {

    public static void start(Context context) {
        Intent starter = new Intent(context, ACSignUp.class);
        context.startActivity(starter);
    }

    VMSignUp vmSignUp;

    EditText etPhone,etCode,etPwd;

    TextView tvSignUp;

    ImageView ivSee;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_sign_up);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("");
        simpleHead.setOnItemclicklistener(() -> finish());

        etPhone =  ZViewHelp.addTextChangedListener(this,R.id.etPhone,this);
        etCode = ZViewHelp.addTextChangedListener(this,R.id.etCode,this);
        etPwd = ZViewHelp.addTextChangedListener(this,R.id.etPwd,this);


        ivSee = ZViewHelp.setOnClickListener(this,R.id.ivSee,this);

        tvSignUp =  ZViewHelp.setOnClickListener(this,R.id.tvSignUp,this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmSignUp = new ViewModelProvider(this).get(VMSignUp.class);
        vmSignUp.liveIsBtnEnable.observe(this,tvSignUp::setEnabled);
        vmSignUp.getLiveErrorToast().observe(this,this::errorToast);
        vmSignUp.getLiveSucceedToast().observe(this,this::succeedToast);
    }

    private void showPassword(boolean show){

        etPwd.setTransformationMethod(show? HideReturnsTransformationMethod.getInstance():
                PasswordTransformationMethod.getInstance());

        ivSee.setSelected(!show);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.ivSee){

            showPassword(ivSee.isSelected());

        }else if (view.getId() == R.id.tvSignUp){

            vmSignUp.requestSignUp();

        }
    }

    @Override
    protected void initData() {
        super.initData();
        showPassword(false);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        vmSignUp.updateInput(etPhone.getText().toString(),etCode.getText().toString(),etPwd.getText().toString());
    }
}
